package cn.cdtu.tibetplan.service.impl;

import java.util.List;
import cn.cdtu.tibetplan.mapper.PolicyMapper;
import cn.cdtu.tibetplan.pojo.PageResult;
import cn.cdtu.tibetplan.pojo.Policy;
import cn.cdtu.tibetplan.pojo.PolicyExample;
import cn.cdtu.tibetplan.pojo.PolicyExample.Criteria;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import cn.cdtu.tibetplan.service.PolicyService;
import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class PolicyServiceImpl implements PolicyService {

    @Autowired
    private PolicyMapper policyMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Policy> findAll() {
        return policyMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Policy> page = (Page<Policy>) policyMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Policy policy) {
        policyMapper.insert(policy);
    }


    /**
     * 修改
     */
    @Override
    public void update(Policy policy) {
        policyMapper.updateByPrimaryKeySelective(policy);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Policy findOne(Long id) {
        return policyMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            policyMapper.deleteByPrimaryKey(id.intValue());
        }
    }

    @Override
    public PageResult findPage(Policy policy, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PolicyExample example = new PolicyExample();
        Criteria criteria = example.createCriteria();
        if (policy != null) {
            if (policy.getTitle() != null && policy.getTitle().length() > 0) {
                criteria.andTitleLike("%" + policy.getTitle() + "%");
            }
            if (policy.getPath() != null && policy.getPath().length() > 0) {
                criteria.andPathLike("%" + policy.getPath() + "%");
            }
            if (policy.getCreatetime() != null && policy.getCreatetime().length() > 0) {
                criteria.andCreatetimeLike("%" + policy.getCreatetime() + "%");
            }
            if (policy.getUpdatetime() != null && policy.getUpdatetime().length() > 0) {
                criteria.andUpdatetimeLike("%" + policy.getUpdatetime() + "%");
            }
            if (policy.getOperator() != null && policy.getOperator().length() > 0) {
                criteria.andOperatorLike("%" + policy.getOperator() + "%");
            }
            if (policy.getOther1() != null && policy.getOther1().length() > 0) {
                criteria.andOther1Like("%" + policy.getOther1() + "%");
            }
            if (policy.getOther2() != null && policy.getOther2().length() > 0) {
                criteria.andOther2Like("%" + policy.getOther2() + "%");
            }
        }
        Page<Policy> page = (Page<Policy>) policyMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
